home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: fast find algorithm
- Date: Sat, 06 Apr 96 21:05:54 GMT
- Organization: none
- Message-ID: <828824754snz@genesis.demon.co.uk>
- References: <Dp8wE6.8DG@cix.compulink.co.uk> <4ju12t$ovh@news.xs4all.nl>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4ju12t$ovh@news.xs4all.nl> falstaff@xs4all.nl "Falstaff" writes:
-
- >setheridge@cix.compulink.co.uk ("Stephen Etheridge") writes:
- >
- >>Hi
- >
- >>Does anyone have an search algoritm faster than a binary chop for the
- >>following:
- >
- >>find a date from a sorted array of 1500 possible storage locations
- >
- >It is theoretically not possible to find an entry in a sorted list
- >in less than ceil(2log(N)) (=11 in this case) operations. bsearch()
- >does just that, so if it isn't fast enough you should try other methods.
-
- That may be true for the worst case but you can do better on average
- if you know something about the distribution of values. An example
- has been posted.
-
- >A plain table lookup is fastest, but can only be done if the number
- >of elements isn't too great
-
- I think you mean the number of possible values of the key.
-
- >-- would not work for dates unless you
- >aren't interested in the year part, or if you have tons of memory
- >(need 31*12*100 elements if you ignore the century).
- >
- >Hashing is slightly slower than straight table lookup and can't be
- >used when you want to delete elements from your table.
-
- It can, you can use a technique like deleted item markers.
-
- >If you want to minimize time to *successfully* searching elements,
- >and a few elements account for>90% of your input, you can sort the
- >array into order of searches and use a linear search.
-
- That leaves you vulnerable to unpleasant worst case behaviour.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-